package com.hsl.housaileibot001.document;

import com.hsl.housaileibot001.model.vo.MockExamVO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.time.LocalDateTime;

/**
 * 模拟题目 MongoDB 文档实体
 */
@Data
@Document(collection = "mock_question")
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "模拟题目")
public class MockQuestion {

    /**
     * 主键ID
     */
    @Id
    @Schema(description = "主键ID")
    private String id;

    /**
     * 关联的面试题ID
     */
    @Schema(description = "关联的面试题ID")
    private Long interviewQuestionId;

    /**
     * 面试题概述
     */
    @Schema(description = "面试题概述")
    private String overview;

    /**
     * 题目列表
     */
    @Schema(description = "题目列表")
    private java.util.List<QuestionItem> questionItems;

    /**
     * 题目状态：ACTIVE-有效，INACTIVE-无效
     */
    @Schema(description = "题目状态")
    private String status;

    /**
     * 创建时间
     */
    @Schema(description = "创建时间")
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @Schema(description = "更新时间")
    private LocalDateTime updatedTime;

    /**
     * 创建人
     */
    @Schema(description = "创建人")
    private Long createdBy;

    /**
     * 更新人
     */
    @Schema(description = "更新人")
    private Long updatedBy;

    /**
     * 备注
     */
    @Schema(description = "备注")
    private String remark;

    /**
     * 题目项静态内部类
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "题目项")
    public static class QuestionItem {

        /**
         * 题目内容
         */
        @Schema(description = "题目内容")
        private String question;

        /**
         * 选项列表
         */
        @Schema(description = "选项列表")
        private java.util.List<Option> options;

        /**
         * 正确答案
         */
        @Schema(description = "正确答案")
        private String answer;

        /**
         * 题目解析
         */
        @Schema(description = "题目解析")
        private String explanation;
    }

    /**
     * 选项静态内部类
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "选项")
    public static class Option {

        /**
         * 选项标识（A、B、C、D等）
         */
        @Schema(description = "选项标识")
        private String optionKey;

        /**
         * 选项内容
         */
        @Schema(description = "选项内容")
        private String optionValue;

        /**
         * 是否为正确答案
         */
        @Schema(description = "是否为正确答案")
        private Boolean isCorrect;
    }

    /**
     * 从 VO 对象创建文档实体
     * @param interviewQuestionId 面试题ID
     * @param mockQuestionVO VO对象
     */
    public MockQuestion(Long interviewQuestionId, MockExamVO.MockQuestion mockQuestionVO) {
        this.interviewQuestionId = interviewQuestionId;
        this.overview = mockQuestionVO.getOverview();
        this.questionItems = convertQuestionItems(mockQuestionVO.getQuestionItems());
        this.status = "ACTIVE";
        this.createdTime = LocalDateTime.now();
        this.updatedTime = LocalDateTime.now();
    }

    /**
     * 转换为 VO 对象
     * @return MockExamVO.MockQuestion
     */
    public MockExamVO.MockQuestion toVO() {
        MockExamVO.MockQuestion vo = new MockExamVO.MockQuestion();
        vo.setOverview(this.overview);
        vo.setQuestionItems(convertToVOQuestionItems(this.questionItems));
        return vo;
    }

    /**
     * 将 VO 中的题目项转换为文档实体题目项
     */
    private java.util.List<QuestionItem> convertQuestionItems(
            java.util.List<MockExamVO.MockQuestion.QuestionItem> voItems) {
        if (voItems == null) {
            return null;
        }
        return voItems.stream()
                .map(voItem -> {
                    QuestionItem item = new QuestionItem();
                    item.setQuestion(voItem.getQuestion());
                    item.setAnswer(voItem.getAnswer());
                    item.setExplanation(voItem.getExplanation());
                    
                    // 转换选项
                    if (voItem.getOptions() != null) {
                        item.setOptions(voItem.getOptions().stream()
                                .map(voOption -> {
                                    Option option = new Option();
                                    option.setOptionKey(voOption.getOptionKey());
                                    option.setOptionValue(voOption.getOptionValue());
                                    option.setIsCorrect(voOption.getIsCorrect());
                                    return option;
                                })
                                .toList());
                    }
                    return item;
                })
                .toList();
    }

    /**
     * 将文档题目项转换为 VO 题目项
     */
    private java.util.List<MockExamVO.MockQuestion.QuestionItem> convertToVOQuestionItems(
            java.util.List<QuestionItem> items) {
        if (items == null) {
            return null;
        }
        return items.stream()
                .map(item -> {
                    MockExamVO.MockQuestion.QuestionItem voItem = new MockExamVO.MockQuestion.QuestionItem();
                    voItem.setQuestion(item.getQuestion());
                    voItem.setAnswer(item.getAnswer());
                    voItem.setExplanation(item.getExplanation());
                    
                    // 转换选项
                    if (item.getOptions() != null) {
                        voItem.setOptions(item.getOptions().stream()
                                .map(option -> {
                                    MockExamVO.MockQuestion.QuestionItem.Option voOption = 
                                            new MockExamVO.MockQuestion.QuestionItem.Option();
                                    voOption.setOptionKey(option.getOptionKey());
                                    voOption.setOptionValue(option.getOptionValue());
                                    voOption.setIsCorrect(option.getIsCorrect());
                                    return voOption;
                                })
                                .toList());
                    }
                    return voItem;
                })
                .toList();
    }
}

